package com.ocean.courtyard.core.worldguard;

import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.domains.DefaultDomain;
import com.sk89q.worldguard.protection.flags.Flags;
import com.sk89q.worldguard.protection.flags.StateFlag;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.managers.storage.StorageException;
import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.sk89q.worldguard.protection.regions.RegionContainer;
import org.bukkit.World;

public class WorldGuardSetting {

    private final WorldGuard worldGuard;

    public WorldGuardSetting(WorldGuard worldGuard) {
        this.worldGuard = worldGuard;
    }

    public void setHomeDefaultFlag(World world, String playerName) {
        // 获取你想要保护的世界
        if (world != null) {
            RegionContainer regionContainer = worldGuard.getPlatform().getRegionContainer();
            com.sk89q.worldedit.world.World adapted = BukkitAdapter.adapt(world);
            RegionManager regionManager = regionContainer.get(adapted);
            if (regionManager != null) {
                // 创建一个覆盖整个世界的区域
                BlockVector3 minPoint = new BlockVector3(-29999984, -320, -29999984);
                BlockVector3 maxPoint = new BlockVector3(29999984, 320, 29999984);

                ProtectedCuboidRegion region = new ProtectedCuboidRegion(world.getName(), false, minPoint, maxPoint);

                // 设置区域的 flag
                region.setFlag(Flags.ENTRY, StateFlag.State.DENY);
                region.setFlag(Flags.ITEM_DROP, StateFlag.State.DENY);
                // 将区域添加到 RegionManager
                regionManager.addRegion(region);
                region.getOwners().addPlayer(playerName);
                try {
                    regionManager.save();
                    regionManager.load();
                } catch (StorageException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    public void setHomeMemberFlag(World world, String playerName) {
        if (world != null) {
            RegionContainer regionContainer = worldGuard.getPlatform().getRegionContainer();
            com.sk89q.worldedit.world.World adapted = BukkitAdapter.adapt(world);
            RegionManager regionManager = regionContainer.get(adapted);
            if (regionManager != null) {
                ProtectedRegion region = regionManager.getRegion(world.getName());
                if (region != null) {
                    DefaultDomain members = region.getMembers();
                    members.addPlayer(playerName);
                    region.setMembers(members);

                    // 设置区域的 flag
                    region.setFlag(Flags.ENTRY, StateFlag.State.ALLOW);
                    region.setFlag(Flags.ITEM_DROP, StateFlag.State.ALLOW);
                    region.setFlag(Flags.BUILD, StateFlag.State.ALLOW);
                    region.setFlag(Flags.USE, StateFlag.State.ALLOW);
                    region.setFlag(Flags.INTERACT, StateFlag.State.ALLOW);
                    region.setFlag(Flags.CHEST_ACCESS, StateFlag.State.ALLOW);
                    region.setFlag(Flags.PVP, StateFlag.State.ALLOW);
                    region.setFlag(Flags.SLEEP, StateFlag.State.ALLOW);
                    region.setFlag(Flags.TNT, StateFlag.State.ALLOW);

                    try {
                        regionManager.save();
                        regionManager.load();
                    } catch (StorageException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    }
}
